package org.zlb.algorithm.utils;

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;

import sun.misc.BASE64Encoder;

public class KeyPairGens {

    private static final String ALGORITHM = "RSA";
    
    private static final int KEYSIZE = 1024;
    
    public static String[] generateKeyPair() throws Exception {
        SecureRandom secureRandom = new SecureRandom();
        
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
        keyPairGenerator.initialize(KEYSIZE, secureRandom);  
        
        KeyPair keyPair = keyPairGenerator.generateKeyPair();  
  
        Key publicKey = keyPair.getPublic();  
        Key privateKey = keyPair.getPrivate();  
  
        byte[] publicKeyBytes = publicKey.getEncoded();  
        byte[] privateKeyBytes = privateKey.getEncoded();  
  
        String publicKeyBase64 = new BASE64Encoder().encode(publicKeyBytes);  
        String privateKeyBase64 = new BASE64Encoder().encode(privateKeyBytes); 

        return new String[]{publicKeyBase64, privateKeyBase64};
    }
}
